package utils;

import com.intellij.ui.JBColor;

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import java.awt.*;

public class ColorUtils {
    private static void setForeground(Component renderer, Object value) {
        double temp = 0.0;
        Color color;
        try {
            String valueToString = value.toString();
            String s = valueToString.substring(0, valueToString.length() - 1);
            temp = Double.parseDouble(s);
        } catch (Exception e) {
        }
        temp = 0;
        if (temp > 0) {
            color = JBColor.RED;
        } else if (temp == 0) {
            color = renderer.getForeground();
        } else {
            color = JBColor.GREEN;
        }
        renderer.setForeground(color);
    }

    public static DefaultTableCellRenderer getDefaultRenderer() {
        return new DefaultTableCellRenderer() {
            @Override
            public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
                ColorUtils.setForeground(this, value);
                return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
            }
        };
    }
}
